<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<head th:include="header">
    <title>字典管理-列表</title>
</head>

<body class="gray-bg">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>字典管理</h5>
                </div>
                <div class="ibox-content">
                    <div class="row row-lg">
                        <div class="col-sm-12">
                            <div class="row">
                                    <div class="col-sm-3">
                                        <input type="text" class="form-control" id="dicName" placeholder="字典名称" />
                                    </div>
                                    <div class="col-sm-3">
                                            <input type="text" class="form-control" id="dicCode" placeholder="字典代码" />
                                        </div>
                                    <div class="col-sm-3">
                                        <button type="submit" class="btn btn-sm btn-primary" onclick="Dic.search()">
                                            <i class="fa fa-search">搜索</i>
                                        </button>
                                    </div>
                                </div>
                            <div class="hidden-xs" id="DicTableToolbar" role="group">
                                <button shiro:hasPermission="/dic/add" class="btn btn-sm btn-primary" type="submit" onclick="Dic.openAddDic()">
                                    <i class="fa fa-pencil"></i>添加</button>
                                <button shiro:hasPermission="/dic/edit" class="btn btn-sm btn-info" type="submit" onclick="Dic.openChangeDic()">
                                    <i class="fa fa-paste"></i>修改</button>
                                <button shiro:hasPermission="/dic/delete" class="btn btn-sm btn-danger" type="submit" onclick="Dic.delDic()">
                                    <i class="fa fa-cut"></i>删除</button>
                            </div>
                            <table id="DicTable" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div th:include="footer"></div>
    <script type="text/javascript" th:inline="javascript">
        /**
         * 角色管理的单例
         */
        var Dic = {
            id: "DicTable",	//表格id
            seItem: null,		//选中的条目
            table: null,
            layerIndex: -1
        };

        /**
         * 初始化表格的列
         */
        Dic.initColumn = function () {
            var columns = [
                { field: 'selectItem', radio: true },
                { title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle' },
                { title: '字典名称', field: 'varName', align: 'center', valign: 'middle', sortable: true, width: '17%' },
                { title: '字典代码', field: 'varCode', align: 'center', valign: 'middle', sortable: true, width: '12%' },
                { title: '字典父编号', field: 'parentId', align: 'center', valign: 'middle', sortable: true},
                {title: '创建时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true},
                {title: '更新时间', field: 'updateTime', align: 'center', valign: 'middle', sortable: true}]
            return columns;
        };


        /**
         * 检查是否选中
         */
        Dic.check = function () {
            var selected = $('#' + this.id).bootstrapTreeTable('getSelections');
            if (selected.length == 0) {
                hdw.info("请先选中表格中的某一记录！");
                return false;
            } else {
                Dic.seItem = selected[0];
                return true;
            }
        };

        /**
         * 点击添加菜单
         */
        Dic.openAddDic = function () {
            var index = layer.open({
                type: 2,
                title: '添加菜单',
                area: ['830px', '450px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: basePath+ 'dic/addPage'
            });
            this.layerIndex = index;
        };

        /**
         * 点击修改
         */
        Dic.openChangeDic = function () {
            if (this.check()) {
                var index = layer.open({
                    type: 2,
                    title: '修改菜单',
                    area: ['800px', '450px'], //宽高
                    fix: false, //不固定
                    maxmin: true,
                    content: basePath+ 'dic/editPage/' + this.seItem.id
                });
                this.layerIndex = index;
            }
        };

        /**
         * 删除
         */
        Dic.delDic = function () {
            if (this.check()) {
                var operation = function () {
                    var ajax = new $ax(basePath+ "dic/delete", function (data) {
                        if(data.success){
                            hdw.success("删除成功!");
                        }else{
                            hdw.error("删除失败!" + data.msg + "!");
                        }
                        Dic.table.refresh();
                    }, function (data) {
                        hdw.error("删除失败!" + data.responseJSON.message + "!");
                    });
                    ajax.set("dicId", Dic.seItem.id);
                    ajax.start();
                };

                hdw.confirm("是否刪除该菜单?", operation);
            }
        };

        /**
         * 搜索
         */
        Dic.search = function () {
            var queryData = {};
            queryData['dicName'] = $("#dicName").val();
            queryData['dicCode'] = $("#dicCode").val();
            Dic.table.refresh({ query: queryData });
        }

        $(function () {
            var defaultColunms = Dic.initColumn();
            var table = new BSTreeTable(Dic.id, basePath + "dic/treeGrid", defaultColunms);
            table.setExpandColumn(2);
            table.setIdField("id");
            table.setCodeField("id");
            table.setParentCodeField("parentId");
            table.setExpandAll(true);
            table.init();
            Dic.table = table;
        });

    </script>
</body>

</html>